package com.atguigu.edu2021.mock.factory;

import com.atguigu.edu2021.mock.bean.CartInfo;
import com.atguigu.edu2021.mock.bean.CourseInfo;
import com.atguigu.edu2021.mock.bean.OrderInfo;
import com.atguigu.edu2021.mock.bean.UserInfo;
import com.atguigu.edu2021.mock.config.AppConfig;
import com.atguigu.edu2021.mock.log.AppCommon;
import com.atguigu.edu2021.mock.log.enums.PageId;
import com.atguigu.edu2021.mock.stage.Stage;
import org.apache.commons.lang3.time.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class UserSession {


    public static final String PURPOSE_STUDY="STUDY";
    public static final String PURPOSE_VISIT="VISIT";

    private static ThreadLocal<UserInfo> userInfoThreadLocal=new ThreadLocal<>();

    private static ThreadLocal<Class<? extends Stage>> nextStageThreadLocal=new ThreadLocal<>();


    private static ThreadLocal<String> sessionIdThreadLocal=new ThreadLocal<>();

    private static ThreadLocal<Date> curDateTimeThreadLocal=new ThreadLocal<>();

    private static ThreadLocal<CourseInfo> curCourseThreadLocal=new ThreadLocal<>();

    private static ThreadLocal<CourseInfo> buyingCourseThreadLocal=new ThreadLocal<>();

    private static ThreadLocal<PageId> lastPageIdThreadLocal=new ThreadLocal<>();

    private static ThreadLocal<AppCommon> appCommonThreadLocal=new ThreadLocal<>();

    private static ThreadLocal<List<CartInfo>> cartListThreadLocal=new ThreadLocal<>();


    private static ThreadLocal<OrderInfo> orderInfoThreadLocal=new ThreadLocal<>();

    private static ThreadLocal<String> purposeThreadLocal=new ThreadLocal<>();

    public static String getPurpose(){
        return purposeThreadLocal.get();
    }

    public static boolean isStudy(){
        if( getPurpose().equals(UserSession.PURPOSE_STUDY)){
            return   true;
        }else{
            return false;
        }

    }

    public static void setPurpose(String purpose){
        purposeThreadLocal.set(purpose);
    }


    public static UserInfo getUserInfo(){
        return userInfoThreadLocal.get();
    }

    public static void setUserInfo(UserInfo userInfo){
        userInfoThreadLocal.set(userInfo);
    }

    public static Class<? extends  Stage> getNextStageInfo(){
        return nextStageThreadLocal.get();
    }

    public static void setNextStageInfo(Class<? extends  Stage>  stageClass){
        nextStageThreadLocal.set(stageClass);
    }

    public static  Date   getCurDateTime(){
        if(AppConfig.if_realtime==1){
            return  new Date();
        }else{
            return curDateTimeThreadLocal.get();
        }

    }

    public static  void setCurDateTime(Date dateTime){
        curDateTimeThreadLocal.set(dateTime);
    }

    public static  void addTimeByDuringTime(Integer duringTime){
        Date date = DateUtils.addMilliseconds(curDateTimeThreadLocal.get(), duringTime );
        curDateTimeThreadLocal.set(date);

    }





    public static CourseInfo getCurCourseInfo(){
        return curCourseThreadLocal.get();
    }

    public static void setCurCourseInfo(CourseInfo courseInfo){
        curCourseThreadLocal.set(courseInfo);
    }

    public static CourseInfo getBuyingCourseInfo(){
        return buyingCourseThreadLocal.get();
    }

    public static void setBuyingCourseInfo(CourseInfo courseInfo){
        buyingCourseThreadLocal.set(courseInfo);
    }



    public static PageId getLastPageId(){
        return lastPageIdThreadLocal.get();
    }
    public static  void setLastPageId(PageId pageId){
        lastPageIdThreadLocal.set(pageId);
    }



    //    private static ThreadLocal<OrderInfo> orderInfoThreadLocal=new ThreadLocal<>();
//
//    public static OrderInfo getOrderInfo(){
//        return orderInfoThreadLocal.get();
//    }
//    public static  void setOrderInfo(OrderInfo orderInfo){
//        orderInfoThreadLocal.set(orderInfo);
//    }
//


    public static AppCommon getAppCommon(){
        return appCommonThreadLocal.get();
    }

    public static void  setAppCommon(AppCommon appCommon){
          appCommonThreadLocal.set(appCommon);
    }
    //

    public static String getSessionId(){
        return sessionIdThreadLocal.get();
    }

    public static void setSessionId(String sessionId){
        sessionIdThreadLocal.set(sessionId);
    }
    //
    public static OrderInfo getOrderInfo(){
        return orderInfoThreadLocal.get();
    }

    public static void setOrderInfo(OrderInfo orderInfo  ){
        orderInfoThreadLocal.set(orderInfo);
    }

    public static List<CartInfo> getCartList(){
        return cartListThreadLocal.get();
    }

    public static  void  setCartList(List<CartInfo> cartList){

        cartListThreadLocal.set(cartList);
    }


    public static  void  addCart(CartInfo cartInfo){
        if(cartListThreadLocal.get()==null){
            cartListThreadLocal.set(new ArrayList<CartInfo>());
        }
        cartListThreadLocal.get().add(cartInfo);
    }
}
